package cn.com.qing.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.Base64;
import java.util.UUID;

/**
 * 
 * @author admin
 *
 */
public class Base64Utils {
	
	
	public static String encodeToString(String value) {
		return Base64.getEncoder().encodeToString(value.getBytes());
	}
	
	public static byte[] encode(byte[] value) {
		return Base64.getEncoder().encode(value);
	}
	
	/**
	 * 
	 * @param filePath
	 * @return
	 */
	public static boolean EncodeFile(String filePath) {
		long MAX_ENCRYPT_BLOCK = 6144;
		String destFile = null;
		try {
			//检测需要加密文件
	    	File file = new File(filePath);
			String path = file.getPath();
			if (!file.exists()) {
				return false;
			}
			
			//初始化文件
			int index = path.lastIndexOf("\\");
			destFile = path.substring(0, index) + File.separatorChar + UUID.randomUUID().toString();
			File dest = new File(destFile);
//			InputStream in = new FileInputStream(filePath);
			RandomAccessFile in = new RandomAccessFile(filePath, "r");
			long length = in.length();
			long offset = 0;
			OutputStream out = new FileOutputStream(destFile);
			byte[] cache;
			// 对数据分段加密
			while (length-offset> 0) {
				byte[] buffer = new byte[(int) MAX_ENCRYPT_BLOCK];
				if(length-offset>MAX_ENCRYPT_BLOCK) {
					buffer = new byte[(int) MAX_ENCRYPT_BLOCK];
				}else {
					buffer = new byte[(int) (length-offset)];
				}
				in.read(buffer);
				cache = encode(buffer);
				out.write(cache, 0, cache.length);
				out.flush();
				offset = offset + MAX_ENCRYPT_BLOCK;
			}
			in.close();
			out.close();
			file.delete();
			dest.renameTo(new File(filePath));
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			if(destFile!=null&&new File(destFile).exists()) {
				new File(destFile).delete();
			}
			return false;
		}
	}
	
	
	public static String decode(String value) {
		return new String(Base64.getDecoder().decode(value));
	}
	
	public static byte[] decode(byte[] value) {
		return Base64.getDecoder().decode(value);
	}
	
	/**
	 * 
	 * @param filePath
	 * @return
	 */
	public static boolean DecodeFile(String filePath) {
		long MAX_DECRYPT_BLOCK = 6144;
		String destFile = null;
		try {
			//检测需要解密文件
	    	File file = new File(filePath);
	    	String path = file.getPath();
			if (!file.exists()) {
				return false;
			}
			//初始化文件
			int index = path.lastIndexOf("\\");
			destFile = path.substring(0, index) + File.separatorChar + UUID.randomUUID().toString();
			File dest = new File(destFile);
			if (!dest.getParentFile().exists()) {
				dest.getParentFile().mkdirs();
			}
//			InputStream in = new FileInputStream(filePath);
			RandomAccessFile in = new RandomAccessFile(filePath, "r");
			long length = in.length();
			long offset = 0;
			OutputStream out = new FileOutputStream(destFile);
			byte[] cache;
			// 对数据分段加密
			while (length-offset> 0) {
				byte[] buffer = new byte[(int) MAX_DECRYPT_BLOCK];
				if(length-offset>MAX_DECRYPT_BLOCK) {
					buffer = new byte[(int) MAX_DECRYPT_BLOCK];
				}else {
					buffer = new byte[(int) (length-offset)];
				}
				in.read(buffer);
				cache = decode(buffer);
				out.write(cache, 0, cache.length);
				out.flush();
				offset = offset + MAX_DECRYPT_BLOCK;
			}
			in.close();
			out.close();
			file.delete();
			dest.renameTo(new File(filePath));
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			if(destFile!=null&&new File(destFile).exists()) {
				new File(destFile).delete();
			}
			return false;
		}
	}
}
